package baseclass;

import java.io.IOException;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.border.EmptyBorder;
import model.entity.ReinsuranceBillingEntity;
import model.entity.ReinsuranceEnsuredEntity;
import jxl.CellView;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.read.biff.BiffException;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

/**
 * @author 50123
 * 
 */
@SuppressWarnings("serial")
public abstract class BaseCreateBill<T extends BaseBillEntity> extends JFrame
	implements Serializable, Runnable
{
    public JProgressBar progressBar=new JProgressBar();
    public SimpleDateFormat sdFormat=new SimpleDateFormat("yyyy-MM-dd");
    public String date="-"+sdFormat.format(new Date())+"-";
    public List<T> bills=new ArrayList<T>();
    public WritableWorkbook writeWorkBook;
    public static ReinsuranceBillingEntity ReinsuranceBill;
    public static ReinsuranceEnsuredEntity ReinsuranceEnsured;
    // 0=QS 1=FT 2=ES 3=TS
    public WritableSheet[] writeSheet=new WritableSheet[4];
    public CellView cellView=new CellView();
    public int row;
    public int column;
    public int type;
    // 設定新開的檔案的字型
    public WritableFont chFont11w=new WritableFont(WritableFont.TIMES,12);
    public WritableCellFormat cellFormat1=new WritableCellFormat();

    public int currentValue;
    public long totalBytes=0;
    public long byteRead=0;

    public BaseCreateBill() throws IOException, WriteException
    {
	super();
	setTitle("Progress");
	setResizable(false);
	setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	setBounds(100,100,326,122);
	JPanel contentPane=new JPanel();
	contentPane.setBorder(new EmptyBorder(5,5,5,5));
	setContentPane(contentPane);
	contentPane.setLayout(null);
	// 視窗至中
	setLocationRelativeTo(null);
	progressBar.setBounds(14,34,290,21);
	// 設置初始值與最大值
	progressBar.setStringPainted(true);
	progressBar.setValue(0);
	progressBar.setMaximum(100);
	contentPane.add(progressBar);
	JLabel lblNewLabel=new JLabel("\u7522\u751F\u4E2D...........");
	lblNewLabel.setBounds(56,13,217,19);
	contentPane.add(lblNewLabel);
	setVisible(true);
	// set cloumn format
	chFont11w.setColour(Colour.BLACK); // 字體黑色
	cellFormat1.setFont(chFont11w); // 讀入剛剛設定的字型
	cellFormat1.setBackground(Colour.WHITE); // 設定CELL背景
	cellFormat1.setAlignment(Alignment.CENTRE); // 設定置中
	cellFormat1.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK); // 設定框線
    }

    public List<T> getBills()
    {
	return bills;
    }

    public void setBills(List<T> bills)
    {
	this.bills=bills;
    }

    @SuppressWarnings("unused")
    public void getTotalbyte()
    {
	for(T bill : bills)
	{
	    this.totalBytes=this.totalBytes+1;
	}
    }

    public void setProgressBar()
    {
	byteRead=byteRead+1;
	currentValue=(int)(byteRead*100/totalBytes);
	progressBar.setValue(currentValue);
    }

    public void setSheet()
    {
	writeSheet[0]=writeWorkBook.createSheet("QS帳單",0);
	writeSheet[1]=writeWorkBook.createSheet("FT帳單",1);
	writeSheet[2]=writeWorkBook.createSheet("ES帳單",2);
	writeSheet[3]=writeWorkBook.createSheet("TS帳單",3);
    }

    abstract public void create() throws BiffException, IOException,
	    WriteException, BiffException, WriteException;

    public abstract void run();

}
